package ScanLineAlghorithms.RectanglesUnion;

import java.util.ArrayList;
import java.util.Hashtable;
import java.util.Stack;

import DataModel.LayoutManager.CEdge;
import DataModel.SegmentTree.SegmentTreeImpl;
import DataModel.SegmentTree.SegmentTreeNode;
import DataModel.SegmentTree.SegmentTreeNodeImpl;
import DataModel.SegmentTree.SegmentTreePostorderIterator;
/**
 * Extention of segment tree class in order to be capable to calculate rectangles union.
 * @author Guy
 *
 */
public class SegmentTreeUnionAlg extends SegmentTreeImpl {

	public SegmentTreeUnionAlg(ArrayList<CEdge> edges, char axis) {
		super(edges, axis);

	}
	@Override
	public boolean insert(CEdge edge) {
		if (!super.insert(edge)) return false;
		return updateAdditionalInfo();
	
	}
	@Override
	public boolean remove(CEdge edge) {
		if (!super.remove(edge)) return false;
		return updateAdditionalInfo();
	
	}
	protected boolean updateAdditionalInfo(){
		Stack<SegmentTreeNode> s = new Stack<SegmentTreeNode>();
		s.push(this.root);
		SegmentTreePostorderIterator it = SegmentTreePostorderIterator.IteratorInitialize(this.root);
		while (it.hasNext()) {
			SegmentTreeNode t = it.next();
			if (t == null) {
				System.err.println("Error in traversing postorder , node is null");
				return false;
			}
			t.getGenericData().update();
			
		}
		return true;
	}
	@Override
	protected SegmentTreeNode createTreeNode(long begin, long end) {
		SegmentTreeNode newNode = new SegmentTreeNodeImpl(begin,end);
		newNode.setGenericData(new TreeNodeAdditionalData(newNode));
		return newNode; 
		
	}
}
